package com.frank.oss.util;

import com.frank.oss.eneity.FileInfo;
import io.minio.*;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Frank Zou
 * @version v0.0.1
 * @date 2024/2/1 10:07
 * @description Minio 操作工具类
 */
@Component
public class MinioUtil {

    private final MinioClient minioClient;

    public MinioUtil(MinioClient minioClient) {
        this.minioClient = minioClient;
    }

    /**
     * 创建 Minio Bucket 桶
     *
     * @param bucketName bucket name
     * @throws Exception exception
     */
    public void createBucket(String bucketName) throws Exception {
        boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (!exists) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
    }

    /**
     * 上传文件到 Minio
     *
     * @param inputStream inputStream
     * @param bucketName  bucket name
     * @param objectName  object name
     * @throws Exception exception
     */
    public void uploadFile(InputStream inputStream, String bucketName, String objectName) throws Exception {
        minioClient.putObject(PutObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .stream(inputStream, -1, Integer.MAX_VALUE)
                .build());
    }

    /**
     * 列出所有的桶
     *
     * @return buckets
     */
    public List<String> listBuckets() throws Exception {
        List<Bucket> buckets = minioClient.listBuckets();
        return buckets.stream().map(Bucket::name).collect(Collectors.toList());
    }

    /**
     * 列出所有文件
     * @param bucketName 桶名称
     * @return 所有的文件
     * @throws Exception exception
     */
    public List<FileInfo> getAllObjects(String bucketName) throws Exception {
        Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build());
        List<FileInfo> files = new LinkedList<>();
        for (Result<Item> result : results){
            FileInfo fileInfo = new FileInfo();
            Item item = result.get();
            fileInfo.setName(item.objectName());
            fileInfo.setSize(item.size());
            fileInfo.setEtag(item.etag());
            fileInfo.setIsDir(item.isDir());
            fileInfo.setLatest(item.isLatest());
            files.add(fileInfo);
        }
        return files;
    }

    /**
     * 下载文件
     * @param bucket 桶名称
     * @param objectName 文件名称
     * @return 文件流
     * @throws Exception exception
     */
    public InputStream download(String bucket, String objectName) throws Exception{
        return  minioClient.getObject(GetObjectArgs.builder().bucket(bucket).object(objectName).build());
    }

    /**
     * 删除桶
     * @param bucketName bucket name
     * @throws Exception exception
     */
    public void deleteBucket(String bucketName) throws Exception {
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    }

    /**
     * 删除桶里面的文件
     * @param bucketName bucket name
     * @param objectName object name
     * @throws Exception exception
     */
    public void deleteObject(String bucketName, String objectName) throws Exception {
        minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
    }


}
